﻿@model IEnumerable<Digital.Contact.Models.UsersModel>

@{
    ViewBag.Title = "Index";
    ViewBag.IsIndex = "class=open";
    Layout = "~/Views/Shared/_LayoutDigital.cshtml";
}



<div class="row">

    <div class="col-md-12">

        <div class="widget">
            <div class="widget-head">
                <div class="pull-left">
                    @using (Html.BeginForm("Index", "Users", FormMethod.Post, new { @class = "form-inline" }))
                    {
                        @Html.AntiForgeryToken()
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="用户名" name="name" value="@ViewBag.Search" />

                        </div>
                        <button type="submit" class="btn btn-default">搜 索</button>

                    }
                </div>
                <div class="pull-right">@Html.ActionLink("创 建", "Edit", null, new { @class = "btn btn-danger" })</div>
                <div class="widget-icons pull-right">

                </div>
                <div class="clearfix"></div>
            </div>

            <div class="widget-content">

                <table class="table table-striped table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>
                                @Html.DisplayNameFor(model => model.Name)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.RegisterDate)
                            </th>
                            <th>控制</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Name)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.RegisterDate)
                                </td>
                                <td>
                                    <a href="~/Users/Edit?Id=@item.ID" class="btn btn-xs btn-warning"><i class="icon-pencil"></i> </a>
                                    <button onclick="javascript:Delete(@item.ID)" data-toggle="modal" class="btn btn-xs btn-danger"><i class="icon-remove"></i> </button>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                <input type="hidden" id="DataId" />
                @Html.AntiForgeryToken()
                <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 class="modal-title">提示消息</h4>
                            </div>
                            <div class="modal-body">

                                <p id="ModelContent">是否删除？</p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" id="CancelButton" class="btn btn-default" data-dismiss="modal" aria-hidden="true">取消</button>
                                <button type="button" id="OkButton" onclick="javascript: DeletePost()" class="btn btn-primary">确 定</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="AlertModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 class="modal-title">提示消息</h4>
                            </div>
                            <div class="modal-body">

                                <p id="AlertContent">是否删除？</p>
                            </div>

                            <div class="modal-footer">
                                <button type="button" onclick="callBack()" id="CancelButton" class="btn btn-default" data-dismiss="modal" aria-hidden="true">确定</button>
                            </div>
                        </div>
                    </div>
                </div>
                <script>
                    var IsRefresh = false;
                    function callBack() {

                        if (IsRefresh) {
                            window.location.reload();
                        }
                    }


                    function Delete(Id) {
                        $("#ModelContent").text("是否删除？");
                        $("#myModal").modal();
                        $("#DataId").val(Id);
                    }
                    function DeletePost() {

                        $('#myModal').modal('hide');
                        $.ajaxAntiForgery({
                            type: "post",
                            data: { Id: $("#DataId").val() },
                            url: "/Users/Delete",
                            success: function (data) {
                                if (data=="true") {
                                    IsRefresh = true;
                                    AlertMsg("ok", "删除成功");

                                }
                                else {
                                    IsRefresh = false;
                                    AlertMsg("error", "删除失败");
                                }
                            }
                        })
                    }



                    function AlertMsg(type, msg) {
                        //if (type == "error") {
                        //    $("#AlertDiv").attr("class", "alert alert-success");
                        //}
                        //else {
                        //    $("#AlertDiv").attr("class", "alert alert-warning");
                        //}
                        $("#AlertContent").text(msg);
                        $("#AlertModal").modal("show");
                    }

                </script>
                
                    <div class="widget-foot">
                        <ul class="pagination pull-right">
                            @{int PrevIndex = 1; int NextIndex = 1;}
                            @{
                            if (@ViewBag.PageIndex == 1)
                            {
                                PrevIndex = 1;
                            }
                            else
                            {
                                PrevIndex = @ViewBag.PageIndex - 1;
                            }
                            if (ViewBag.PageCount == ViewBag.PageIndex)
                            {
                                NextIndex = @ViewBag.PageIndex;
                            }
                            else
                            {
                                NextIndex = @ViewBag.PageIndex + 1;
                            }
                            }
                            @if (@ViewBag.PageCount > 1)
                            {
                                <li><a href="~/Users/Index/@PrevIndex?name=@ViewBag.Search">Prev</a></li>
                            }
                            @for (int i = 1; i <= @ViewBag.PageCount; i++)
                            {
                                if (ViewBag.PageIndex == i)
                                {
                                    <li class="active">
                                        <a href="~/Users/Index/@i?name=@ViewBag.Search">@i</a>
                                    </li>
                                }
                                else
                                {
                                    <li>
                                        <a href="~/Users/Index/@i?name=@ViewBag.Search">@i</a>
                                    </li>
                                }

                            }
                            @if (@ViewBag.PageCount > 1 && ViewBag.PageIndex != @ViewBag.PageCount)
                            {
                                <li><a href="~/Users/Index/@NextIndex?name=@ViewBag.Search">Next</a></li>
                            }
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                
            </div>
        </div>
    </div>
</div>
